package com.example.android2019.ch6service;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.android2019.R;

public class ImageActivity extends AppCompatActivity implements View.OnClickListener {
    Button btn;
    TextView tv;
    /*用于主线程与子线程通信*/
    static Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image);
        btn = findViewById(R.id.ch6_image_btn1);
        tv = findViewById(R.id.ch6_image_tv);
        btn.setOnClickListener(this);
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.arg1==1){
                    tv.setText("图片下载完毕");
                }
            }
        };

        /*获取传递的数据*/
        Intent intent = this.getIntent();
        String a = intent.getStringExtra("test");
        Toast.makeText(this, a, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View v) {
        /* 在LogCat中打印 当前的线程名*/
        Log.v("image", "Activity:" + Thread.currentThread().getName());
        /* 开启服务，让服务去做下载图片的任务*/
        Intent intent = new Intent(ImageActivity.this, MyImageService.class);
        startService(intent);
    }
}
